package com.question.controller;

import com.question.constant.JwtClaimsConstant;
import com.question.pojo.dto.UserDTO;
import com.question.pojo.dto.UserLoginDTO;
import com.question.pojo.dto.UserRegisterDTO;
import com.question.pojo.dto.UserRepasswordDTO;
import com.question.pojo.entity.Major;
import com.question.pojo.entity.User;
import com.question.pojo.vo.UserLoginVO;
import com.question.properties.JwtProperties;
import com.question.pojo.result.Result;
import com.question.service.MajorService;
import com.question.service.UserService;
import com.question.utils.JwtUtil;
import com.question.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/users")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MajorService majorService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录：{}", userLoginDTO);

        User user = userService.login(userLoginDTO);

        //登录成功，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getUserId());
        claims.put(JwtClaimsConstant.USERNAME, user.getUsername());
        claims.put(JwtClaimsConstant.IS_ADMIN, user.isAdmin());

        String token = JwtUtil.createJWT(
                jwtProperties.getSecretKey(),
                jwtProperties.getTtl(),
                claims);

        //把token存储到redis中
        redisUtil.setCacheObject(token, token, 24L, TimeUnit.HOURS);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .userId(user.getUserId())
                .userName(user.getUsername())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result<String> logout(HttpServletRequest request) {
        log.info("退出登录");

        String token = request.getHeader(jwtProperties.getTokenName());
        userService.logout(token);

        return Result.success();
    }

    /**
     * 注册
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("用户注册:{}", userRegisterDTO);
        userService.register(userRegisterDTO);
        return Result.success();
    }

    /**
     * 修改信息
     * @return
     */
    @PutMapping( "/modifyinfo")
    @ApiOperation("修改信息")
    public Result modifyInfo(@RequestBody UserDTO userDTO) {
        log.info("修改信息:{}", userDTO);
        userService.modifyInfo(userDTO);
        return Result.success();
    }

    /**
     * 修改密码
     * @param userRepasswordDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result editPWD(@RequestBody UserRepasswordDTO userRepasswordDTO, HttpServletRequest request){
        log.info("修改密码：{}", userRepasswordDTO);

        String token = request.getHeader(jwtProperties.getTokenName());
        userService.editPassword(userRepasswordDTO, token);

        return Result.success();
    }

    /**
     * 查询当前用户信息
     * @return
     */
    @GetMapping
    @ApiOperation("查询当前用户信息")
    public Result<User> getInfo() {
        log.info("查询当前用户信息");
        User user = userService.getById();
        return Result.success(user);
    }

    /**
     * 获取所有专业
     * @return
     */
    @GetMapping("/majors")
    @ApiOperation("获取所有专业")
    public Result<List<Major>> getAllMajors() {
        List<Major> majors = majorService.getAllMajors();
        return Result.success(majors);
    }

}
